<?php

class Account 
{
	/* @var $_accountDAO AccountDAO */
	private $_accountDAO;
	
	/**
	 * Constructer for the class.
	 * 
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		
		$this->_accountDAO = App::getDAO('account', 'account');	
	}
	
	/**
	 * Returns Identity of currently logged in user
	 */
	public static function getIdentity()
	{
		$identity = Zend_Auth::getInstance()->getIdentity();
		if($identity == null) {
			$identity['group_id'] = Acl::getGuestGroupId();
		}
		return $identity;
	}
	
	/**
	 * This Method will handel the Sign up/Create New Account functionallity.
	 * 
	 * @access public
	 * @param  array account
	 * @return int
	 */	
	public function createAccount(array $account) 
	{
		var_dump($account);
		$account["password"] = md5($account["password"]);
		
		$accountId = $this->_accountDAO->createAccount($account);
	}

	/**
	 * This Method is used to get the account details as per acountId.
	 * 
	 * @access public
	 * @param  int $accountId
	 * 
	 * @return array
	 */
	public function getAccount($accountId) 
	{
		return $this->_accountDAO->getAccount($accountId);
	}
	
	/**
	 * This Method is used to get the account details as per user email.
	 * 
	 * @access public
	 * @param string $email
	 * @return array
	 */
	public function getAccountByEmail($email) 
	{
		return $this->_accountDAO->getAccountByEmail($email);
	}

	/**
	 * Used to Suspend an account.
	 * 
	 * @access public
	 * @param int $accountId
	 * @return int
	 */
	public function suspendAccount($accountId) 
	{
		return $this->_accountDAO->suspendAccount($accountId);
	}

	/**
	 * Used to Activated a suspended account
	 * 
	 * @param int userId
	 * @return int
	 */
	public function activateAccount($accountId) 
	{
		return $this->_accountDAO->activateAccount($accountId);
	}

	/**
	 * This method is used for login functionality.
	 * 
	 * @access public
	 * @param string $email
     * @param string $password
	 * @return string 
	 */
	public function login($email, $password, $remember = null) 
	{
		//TODO Implement other Zend_Auth methods to validate login
		$auth        = Core_Auth::getInstance();
		$dbAdapter   = Zend_Db_Table::getDefaultAdapter();
		$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
		
		$authAdapter->setTableName('view_accounts')
				    ->setIdentityColumn('email')
			        ->setCredentialColumn('passwd')
			        ->setCredentialTreatment('md5');
		
		$authAdapter->setIdentity($email)
					->setCredential(md5($password));	
						   					
		$result = $auth->authenticate($authAdapter);
		
		if ($result->isValid()) {
			if ($remember) $this->_rememberMe();
			$authStorage = $auth->getStorage();
			$authStorage->write($this->getAccountByEmail($auth->getIdentity()));
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Remember user for specific period 
	 */
	private function _rememberMe()
	{
		//TODO Need to provide more secure functionality
		$seconds = 60 * 60 * 24 * 7; // 7 days
		Zend_Session::rememberMe($seconds);
	}

	/**
	 * This Method is used to update the user password on forgot Password request.
	 * 
	 * @access public
	 * @param string email
	 */
	public function forgotPassword($email) 
	{
		if('' != $email)
		{
			require_once 'modules/account/models/DAO/UserDAO.php';
			$userDAO = new UserDAO();
			
			$accountId    = $userDAO->getUserIdByEmail($email);
			$newPassword  = $this->generatePassword();
			
			$accountArray = array('password'   => $newPassword);
			
			$result = $this->_accountDAO->updateAccount($accountId, $accountArray);
			
			if(1 == $result)
			{
				return $newPassword;
			}
		}
		
		return null; 
	}
	
	/**
	 * This Method is used to Generate a random 6 Digit alpha-numeric password.
	 * 
	 * @access public
	 * @return string
	 */
	public function generatePassword()
	{
		$randomPassword = md5(time());
		$password = substr($randomPassword,rand(5, 15),6);
		
		return $password;
	}
	
	/**
	 * This method is used to validate the password field and Account existant.
	 * 
	 * @param array $data
	 * @return string
	 */
	public function validation(array $data)
	{
		if(strcmp($data["email"], $data["emailConfirm"])!=0)
		{
			$error['emailError'] = "Emails do not match";		
		}
		
		if(strcmp($data["passwd"], $data["passwordConfirm"])!=0)
		{
			$error['passwordError'] = "Passwords do not match";
		}
		
		if(1 == $this->isAccountExist($data['email']))
		{
			$error['AccountExistError'] = 'Email id exists, Please choose a different email Id';
		}
		
		return $error;
	}
	
	/**
	 * This method is used to check the account existance. 
	 * 
	 * @access public
	 * @param int
	 */
	public function isAccountExist($email)
	{
		return $this->_accountDAO->isAccountExist($email);
	}
	
	/**
	 * Logout Method
	 * 
	 * @access public
	 * @return void
	 */
	public function logout()
	{
		Zend_Auth::getInstance()->clearIdentity();
	}
}
?>